package com.neusoft.data.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.neusoft.data.domain.SerKeyQuestion;
import com.neusoft.data.service.ISerKeyQuestionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 关键问题Controller
 * 
 * @author slh
 * @date 2023-12-29
 */
@RestController
@RequestMapping("/courseSelect/question")
public class SerKeyQuestionController extends BaseController
{
    @Autowired
    private ISerKeyQuestionService serKeyQuestionService;

    /**
     * 查询关键问题列表
     */
    @PreAuthorize("@ss.hasPermi('courseSelect:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(SerKeyQuestion serKeyQuestion)
    {
        startPage();
        List<SerKeyQuestion> list = serKeyQuestionService.selectSerKeyQuestionList(serKeyQuestion);
        return getDataTable(list);
    }

    /**
     * 导出关键问题列表
     */
    @PreAuthorize("@ss.hasPermi('courseSelect:question:export')")
    @Log(title = "关键问题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SerKeyQuestion serKeyQuestion)
    {
        List<SerKeyQuestion> list = serKeyQuestionService.selectSerKeyQuestionList(serKeyQuestion);
        ExcelUtil<SerKeyQuestion> util = new ExcelUtil<SerKeyQuestion>(SerKeyQuestion.class);
        util.exportExcel(response, list, "关键问题数据");
    }

    /**
     * 获取关键问题详细信息
     */
    @PreAuthorize("@ss.hasPermi('courseSelect:question:query')")
    @GetMapping(value = "/{questionId}")
    public AjaxResult getInfo(@PathVariable("questionId") Long questionId)
    {
        return success(serKeyQuestionService.selectSerKeyQuestionByQuestionId(questionId));
    }

    /**
     * 新增关键问题
     */
    @PreAuthorize("@ss.hasPermi('courseSelect:question:add')")
    @Log(title = "关键问题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SerKeyQuestion serKeyQuestion)
    {
        return toAjax(serKeyQuestionService.insertSerKeyQuestion(serKeyQuestion));
    }

    /**
     * 修改关键问题
     */
    @PreAuthorize("@ss.hasPermi('courseSelect:question:edit')")
    @Log(title = "关键问题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SerKeyQuestion serKeyQuestion)
    {
        return toAjax(serKeyQuestionService.updateSerKeyQuestion(serKeyQuestion));
    }

    /**
     * 删除关键问题
     */
    @PreAuthorize("@ss.hasPermi('courseSelect:question:remove')")
    @Log(title = "关键问题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{questionIds}")
    public AjaxResult remove(@PathVariable Long[] questionIds)
    {
        return toAjax(serKeyQuestionService.deleteSerKeyQuestionByQuestionIds(questionIds));
    }

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SerKeyQuestion> util = new ExcelUtil<SerKeyQuestion>(SerKeyQuestion.class);
        List<SerKeyQuestion> userList = util.importExcel(file.getInputStream());
        LoginUser loginUser = getLoginUser();
        String operName = loginUser.getUsername();
        String message = serKeyQuestionService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SerKeyQuestion> util = new ExcelUtil<>(SerKeyQuestion.class);
        util.importTemplateExcel(response, "聊天数据");
    }
}
